import sys
import gc

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class FixedComboBox(QComboBox):
    def __init__(self, parent=None):
        super(FixedComboBox, self).__init__(parent)
        screen_width = QDesktopWidget().screenGeometry().width()
        self.adjusted_size = screen_width // 4.5

    def sizeHint(self):
        # This and the one below should adjust to screen size
        return self.minimumSizeHint()

    def minimumSizeHint(self):
        return QSize(self.adjusted_size, 32)

    def wheelEvent(self, QWheelEvent):
        # Disable mouse wheel scrolling in the ComboBox
        return

class BookToolBar(QToolBar):

    def __init__(self, parent=None):
        # 初始化 ToolBar 样式
        super(BookToolBar, self).__init__(parent)
        self.parent = parent
        self.setMovable(False)
        self.setIconSize(QSize(22, 22))
        self.setFloatable(False)

        self.download = QAction(self.parent.imageFactory.getImage('filesaveas'), "下载全文", self)
        self.lastChapter = QAction(self.parent.imageFactory.getImage('arrow-up'), "上一章", self)
        self.nextChapter = QAction(self.parent.imageFactory.getImage('arrow-down'), "下一章", self)
        self.addAction(self.download)
        self.addAction(self.lastChapter)
        self.addAction(self.nextChapter)

        spacer = QtWidgets.QWidget()
        spacer.setSizePolicy(
            QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)

        # 添加左对齐的组件

        # 创建目录下拉列表
        self.tocBox = FixedComboBox()
        self.tocTreeView = QtWidgets.QTreeView(self.tocBox)
        self.tocBox.setView(self.tocTreeView)
        self.tocTreeView.setItemsExpandable(False)
        self.tocTreeView.setRootIsDecorated(False)

        # 添加 padding 空白实现右对齐
        self.addWidget(spacer)
        self.tocBoxAction = self.addWidget(self.tocBox)

class LibraryToolBar(QToolBar):

    def __init__(self, parent=None):
        # 初始化 ToolBar 样式
        super(LibraryToolBar, self).__init__(parent)
        self.parent = parent
        self.setMovable(False)
        self.setMovable(False)
        self.setIconSize(QSize(22, 22))
        self.setFloatable(False)

